package com.sky.service.impl;

import com.sky.context.BaseContext;
import com.sky.dto.ShoppingCartDTO;
import com.sky.entity.Dish;
import com.sky.entity.Setmeal;
import com.sky.entity.ShoppingCart;
import com.sky.mapper.DishMapper;
import com.sky.mapper.SetmealMapper;
import com.sky.mapper.ShoppingCartMapper;
import com.sky.service.ShoppingCartService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;

@Service
@Slf4j
public class ShoppingCartServiceImpl implements ShoppingCartService {

    @Autowired
    private ShoppingCartMapper shoppingCartMapper;
    @Autowired
    private DishMapper dishMapper;
    @Autowired
    private SetmealMapper setmealMapper;
    /**
     * 添加购物车
     * @param shoppingCartDTO
     */
    @Override
    public void addShoppingCart(ShoppingCartDTO shoppingCartDTO) {
        ShoppingCart shoppingCart = new ShoppingCart();
        BeanUtils.copyProperties(shoppingCartDTO,shoppingCart);
        Long userId = BaseContext.getCurrentId();
        shoppingCart.setUserId(userId);

      List<ShoppingCart> shoppingCarts =  shoppingCartMapper.list(shoppingCart);
      //如果存在,数量加一
      if(shoppingCarts != null && shoppingCarts.size()>0){
          ShoppingCart shoppingCart1 = shoppingCarts.get(0);
          shoppingCart1.setNumber(shoppingCart1.getNumber()+1);
          shoppingCartMapper.updateNumber(shoppingCart1);
      } else {
          //如果不存在，插入数据，数量就是1

          //判断当前添加到购物车的是菜品还是套餐
          Long dishId = shoppingCartDTO.getDishId();
          if (dishId != null) {
              //添加到购物车的是菜品
              Dish dish = dishMapper.getById(dishId);
              shoppingCart.setName(dish.getName());
              shoppingCart.setImage(dish.getImage());
              shoppingCart.setAmount(dish.getPrice());
          } else {
              //添加到购物车的是套餐
              Setmeal setmeal = setmealMapper.getById(shoppingCartDTO.getSetmealId());
              shoppingCart.setName(setmeal.getName());
              shoppingCart.setImage(setmeal.getImage());
              shoppingCart.setAmount(setmeal.getPrice());
          }
          shoppingCart.setNumber(1);
          shoppingCart.setCreateTime(LocalDateTime.now());
          shoppingCartMapper.insert(shoppingCart);


      }
    }

    @Override
    public List<ShoppingCart> showShoppingCart() {
        ShoppingCart shoppingCart = new ShoppingCart();
        Long currentId = BaseContext.getCurrentId();
        shoppingCart.setUserId(currentId);
        List<ShoppingCart> list = shoppingCartMapper.list(shoppingCart);
        return list;
    }

    @Override
    public void cleanShoppingCart() {
        Long currentId = BaseContext.getCurrentId();

        shoppingCartMapper.deleteUserId(currentId);
    }

    @Override
    public void subShoppingCart(ShoppingCartDTO shoppingCartDTO) {
       ShoppingCart shoppingCart = new ShoppingCart();
       BeanUtils.copyProperties(shoppingCartDTO,shoppingCart);

        Long userId = BaseContext.getCurrentId();
        shoppingCart.setUserId(userId);
        List<ShoppingCart> lists = shoppingCartMapper.list(shoppingCart);
        if(lists != null && lists.size()>0){
            ShoppingCart shoppingCart1 = lists.get(0);
            Integer number = shoppingCart1.getNumber();
            if (number == 1){
                shoppingCartMapper.deleteById(shoppingCart1.getId());
            }else {
                shoppingCart1.setNumber(shoppingCart1.getNumber()-1);
                shoppingCartMapper.updateNumber(shoppingCart1);
            }


        }



    }
}
